<template>
  <!-- // 分配角色 -->
  <div>
    <!-- 选中id 勾选 -->
    <el-checkbox-group v-model="roleIds">
      <!--label是当前选中的值 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 保存选中角色的id
      list: []
    }
  },
  created() {
    this.loadRoles()
    this.loadRolesId()
  },
  methods: {
    // 根据id 获取角色
    async loadRolesId() {
      const res = await getUserDetailById(this.id) // 传入id
      console.log(res.data, '获取的id 信息')
      // 保存 选中角色的id
      this.roleIds = res.data.roleIds
    },
    // 获取列表
    async  loadRoles() {
      const res = await getRoles({ page: 1, pagesize: 1000 })
      // console.log('角色列表', res.data.rows)
      this.list = res.data.rows
    },

    // 保存角色列表
    async submit() {
      const res = await assignRoles({ id: this.id, roleIds: this.roleIds })
      console.log(res.data, '保存角色列表---------')
      // 提示父组件 关闭弹窗
      this.$emit('success')
    },
    // 点击取消
    closeDialog() {
    }
  }
}
</script>
